//
//  RefreshTopView.m
//
//  Copyright (c) 2014 YDJ ( https://github.com/ydj/RefreshControl )
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.



#import "RefreshTopView.h"
#import "CTAppLanguage.h"
#import "CTHeader.h"

@interface RefreshTopView ()
{
    UIImageView * loadingImageView;
}

@end


@implementation RefreshTopView


- (void)layoutSubviews
{
    [super layoutSubviews];
    
}

- (void)dealloc
{
    loadingImageView = nil;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        [self initViews];
    }
    
    return self;
}

- (void)resetLayoutSubViews
{
    
    NSArray * temp=self.constraints;
    if ([temp count]>0)
    {
        [self removeConstraints:temp];
    }

    
}

- (void)initViews
{
    

    _activityIndicatorView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    _activityIndicatorView.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH - 40), self.frame.size.height - 25, 0, 0);
    _activityIndicatorView.hidesWhenStopped=YES;
    _activityIndicatorView.color=[UIColor orangeColor];
    [self addSubview:_activityIndicatorView];
    
   
    
    NSArray * gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"loading_two"],[UIImage imageNamed:@"loading_one"], [UIImage imageNamed:@"loading_three"],nil];
    _imageView=[[UIImageView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH - 48)/2.0,51, 45, 48)];

    [self addSubview:_imageView];
    _imageView.image = [UIImage imageNamed:@"loading_two"];
    _imageView.animationImages = gifArray;
    _imageView.animationDuration = 0.5;
    _imageView.animationRepeatCount = -1;
   
    
    loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH - 77)/2.0, 112, 77, 15)];
    loadingImageView.image = [UIImage imageNamed:@"songkaishuaxin_font"];
    [self addSubview:loadingImageView];
    
    
    _promptLabel=[[UILabel alloc] initWithFrame:CGRectMake(30+_imageView.frame.size.width+15, _imageView.frame.origin.y+5,90 , 16)];
    _promptLabel.backgroundColor=[UIColor clearColor];
    _promptLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0  blue:164/255.0  alpha:1.0];
    _promptLabel.font=[UIFont systemFontOfSize:16];
//   _promptLabel.translatesAutoresizingMaskIntoConstraints=NO;
    [self addSubview:_promptLabel];

    
    [self resetViews];
    
}

- (void)resetViews
{
   // _imageView.hidden=NO;
//    [UIView animateWithDuration:0.25 animations:^{
//        _imageView.transform=CGAffineTransformIdentity;
//    }];
//    if ([_activityIndicatorView isAnimating])
//    {
//        [_activityIndicatorView stopAnimating];
//    }
    
    
    
    
//    _promptLabel.text=[CTAppLanguage localizedStringForKey:@"refresh_control_pulldown_one" withValue:nil withTable:@"Language"];
    
}

- (void)canEngageRefresh
{
//    _promptLabel.text=[CTAppLanguage localizedStringForKey:@"refresh_control_pulldown_one" withValue:nil withTable:@"Language"];
//    [UIView animateWithDuration:0.25 animations:^{
//        _imageView.transform=CGAffineTransformMakeRotation(M_PI);
//    }];
    
}

- (void)didDisengageRefresh
{
    [self resetViews];
}

- (void)startRefreshing
{
   // _imageView.hidden=YES;
//    [UIView animateWithDuration:0.25 animations:^{
//        _imageView.transform=CGAffineTransformIdentity;
//    }];
 //   [_activityIndicatorView startAnimating];
   
//    _promptLabel.text=[CTAppLanguage localizedStringForKey:@"refresh_control_pulldown_two" withValue:nil withTable:@"Language"];
    
    loadingImageView.image = [UIImage imageNamed:@"loading_font"];
    
     [_imageView startAnimating];
    
}

- (void)finishRefreshing
{
 //   [_activityIndicatorView stopAnimating];
    loadingImageView.image = [UIImage imageNamed:@"songkaishuaxin_font"];
     [_imageView stopAnimating];
    
    [self resetViews];
    
}




@end

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com 
